package com.light.roller;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.Scene.IOnSceneTouchListener;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.extension.input.touch.controller.MultiTouch;
import org.anddev.andengine.extension.input.touch.controller.MultiTouchException;
import org.anddev.andengine.extension.input.touch.detector.PinchZoomDetector;
import org.anddev.andengine.extension.input.touch.detector.PinchZoomDetector.IPinchZoomDetectorListener;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.input.touch.detector.ScrollDetector;
import org.anddev.andengine.input.touch.detector.SurfaceScrollDetector;
import org.anddev.andengine.input.touch.detector.ScrollDetector.IScrollDetectorListener;

import com.badlogic.gdx.math.Vector2;
import com.huw.cameramanagers.BlackStartEndActivity;
import com.huw.debuger.FileIO;
import com.light.buttons.ChooseStageButton;
import com.light.music.BackGroundMusicController;

public class Choose1 extends BlackStartEndActivity implements IOnSceneTouchListener, IScrollDetectorListener, IPinchZoomDetectorListener {
	
	ChooseStageButton[] chooseStageButtons = {
			new ChooseStageButton(this, 50,200,Roller_T1.class),
			new ChooseStageButton(this, 200,200,Roller_T2.class),
			new ChooseStageButton(this, 350,200,Roller_2.class),
			new ChooseStageButton(this, 500,200,Roller_1.class),
			new ChooseStageButton(this, 650,200,Roller_S10.class),
			new ChooseStageButton(this, 50,350,Roller_3.class),
			new ChooseStageButton(this, 200,350,Roller_S3.class),
			new ChooseStageButton(this, 350,350,Roller_S11.class),
			new ChooseStageButton(this, 500,350,Roller_S4.class),
			new ChooseStageButton(this, 650,350,Roller_S6.class),
			new ChooseStageButton(this, 50,500,Roller_S5.class),
			new ChooseStageButton(this, 200,500,Roller_S7.class),
			new ChooseStageButton(this, 350,500,Roller_S8.class),
			new ChooseStageButton(this, 500,500,Roller_S9.class),
			new ChooseStageButton(this, 650,500,Roller_S12.class),
			new ChooseStageButton(this, 50,650,Roller_S13.class),
			new ChooseStageButton(this, 200,650,Roller_S14.class),
			new ChooseStageButton(this, 350,650,Roller_S15.class),
			new ChooseStageButton(this, 500,650,Roller_S16.class),
			new ChooseStageButton(this, 650,650,Roller_S17.class)
	};
	//private static int unLockedStageNum = 3;
	
	@Override
	public Engine onLoadEngine() {
		
		Vector2 cameraCenter = new Vector2(Roller.CAMERA_WIDTH/2+50, Roller.CAMERA_HEIGHT/2+175);

		Engine engine = initialize(cameraCenter);
		
		return engine;
	}
	
	public Engine initialize(Vector2 cameraCenter)
	{
		
		Engine temp = super.onLoadEngine();
		camera.setCenter(cameraCenter.x, cameraCenter.y);

		
		for(int i=0;i<chooseStageButtons.length;i++)
		{
			chooseStageButtons[i].setStageNum(i+1);
		}      
        
        
        int tempFileIOValue = FileIO.LoadGame(this,FileIO.FileName.OpenNum);            
        
        for(int i=0;i<tempFileIOValue;i++)
        {
        	chooseStageButtons[i].setLocked(false);
        } 
            
        
        return temp;
	}

	@Override
	public void onLoadResources() {
		
		super.onLoadResources();
		
		for(int i=0;i<chooseStageButtons.length;i++)
		{
			chooseStageButtons[i].onLoadResources();
		}   		
	}
	
	private SurfaceScrollDetector mScrollDetector;
	private PinchZoomDetector mPinchZoomDetector;

	@Override
	public Scene onLoadScene() {
		this.mEngine.registerUpdateHandler(new FPSLogger());
		
		Scene scene = super.onLoadScene();
		for(int i=0;i<chooseStageButtons.length;i++)
		{
			chooseStageButtons[i].onLoadScene(scene, Roller.physicsWorld);
		}   		

		BlackSceneCover();		
		
		this.mScrollDetector = new SurfaceScrollDetector(this);
		if(MultiTouch.isSupportedByAndroidVersion()) {
			try {
				this.mPinchZoomDetector = new PinchZoomDetector(this);
			} catch (final MultiTouchException e) {
				this.mPinchZoomDetector = null;
			}
		} else {
			this.mPinchZoomDetector = null;
		}

		scene.setOnSceneTouchListener(this);
		scene.setTouchAreaBindingEnabled(true);
		
		return scene;
	}

	@Override
	public void onLoadComplete() {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	protected void onPause() {
	    super.onPause(); 
	    BackGroundMusicController.pause(); 
	}

	@Override
	public void onScroll(ScrollDetector pScollDetector, TouchEvent pTouchEvent,
			float pDistanceX, float pDistanceY) {
		camera.offsetCenter(-pDistanceX , -pDistanceY);
		
	}

	@Override
	public void onPinchZoomStarted(PinchZoomDetector pPinchZoomDetector,
			TouchEvent pSceneTouchEvent) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPinchZoom(PinchZoomDetector pPinchZoomDetector,
			TouchEvent pTouchEvent, float pZoomFactor) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPinchZoomFinished(PinchZoomDetector pPinchZoomDetector,
			TouchEvent pTouchEvent, float pZoomFactor) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
		if(this.mPinchZoomDetector != null) {
			this.mPinchZoomDetector.onTouchEvent(pSceneTouchEvent);

			if(this.mPinchZoomDetector.isZooming()) {
				this.mScrollDetector.setEnabled(false);
			} else {
				if(pSceneTouchEvent.isActionDown()) {
					this.mScrollDetector.setEnabled(true);
				}
				this.mScrollDetector.onTouchEvent(pSceneTouchEvent);
			}
		} else {
			this.mScrollDetector.onTouchEvent(pSceneTouchEvent);
		}

		return true;
	}

}
